using System;
using System.Collections.Generic;
using System.Text;
using Barrage.GUI.Elements;
using Barrage.GUI;
using Spacedash.Logic;

namespace Spacedash.Game.Subgames
{
    static class SelectProfileElements
    {
        public const string ExitButton = "EXIT_BUTTON";
        public const string ButtonList = "BUTTON_LIST";
        public const string NextButton = "SELECT_BUTTON";
        public const string ProfileSelector = "PROFILE_SELECT";
        public const string SelectProfileTitle = "SELECT_PROFILE_PLAYER";
    }
    class SelectProfileSection : GUISection
    {
        public override int Id
        {
            get { return (int)GameState.NewGameMenu; }
        }

        RaceCreationParams rcp;
        int currentPlayer;

        public SelectProfileSection(SpacedashGame parent,RaceCreationParams rcp, int currentPlayer)
            : base("SelectProfile", parent)
        {
            this.rcp = rcp;
            this.currentPlayer = currentPlayer;
            ((GUIButtonList)Layout.Elements[SelectProfileElements.ButtonList]).Title = Barrage.Language.LanguageManager.Texts[SelectProfileElements.SelectProfileTitle].Replace("%1", (currentPlayer+1).ToString());
            RegisterButtonEvent(SelectProfileElements.ExitButton, new GUIButtonHandler(ExitButtonClick));
            RegisterButtonEvent(SelectProfileElements.NextButton, new GUIButtonHandler(NextButtonClick));
            FillProfiles();
        }

        void FillProfiles()
        {
            GUISelector selector = ((GUISelector)Layout.Elements[SelectProfileElements.ProfileSelector]);

            int i = 0;
            foreach (string name in Profile.GetAvailableProfiles())
            {
                selector.addOption(i++, name);
            }
        }

        public void NextButtonClick(string buttonId)
        {
            rcp.setProfile(currentPlayer,Profile.Load(((GUISelector)Layout.Elements[SelectProfileElements.ProfileSelector]).CurrentOptionValue));
            if (rcp.PlayerNumber > (currentPlayer + 1))
                Parent.NextGameSection = new SelectProfileSection((SpacedashGame)Parent, rcp, currentPlayer+1);
            else
                Parent.NextGameSection = new RaceSection(new Race(rcp), (SpacedashGame)Parent);
        }

        public void ExitButtonClick(string buttonId)
        {
            Parent.NextGameSectionId = (int)GameState.MainMenu;
        }
    }
}
